package cn.tianjindong.blog_portal.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogArticleCategoryMapper;
import cn.tianjindong.blog.mapper.BlogArticleMapper;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleCategory;
import cn.tianjindong.blog.pojo.BlogArticleCategoryExample;
import cn.tianjindong.blog.pojo.BlogArticleExample;
import cn.tianjindong.blog.pojo.BlogArticleExample.Criteria;
import cn.tianjindong.blog.pojo.BlogNotice;
import cn.tianjindong.blog.pojo.BlogNoticeExample;
import cn.tianjindong.blog_portal.service.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private BlogArticleCategoryMapper categoryMapper;

	@Autowired
	private BlogArticleMapper articleMapper;

	@Override
	public List<BlogArticleCategory> getCategoryList() {
		BlogArticleCategoryExample example = new BlogArticleCategoryExample();
		example.setOrderByClause("level");
		List<BlogArticleCategory> list = categoryMapper.selectByExample(example);
		return list;
	}

	@Override
	public PageView<BlogArticle> getArticlePageViewByCategory(Integer currentPage, Integer countItem,String alias) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogArticle> list = articleMapper.getBriefListByCategoryAlias(startIndex, countItem,alias);
		// 获取数据库总条数
		int totalItem = articleMapper.countArticleByCategoryAlias(alias);

		PageView<BlogArticle> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);

		return pageView;
	}

	@Override
	public String getCategoryNameByAlias(String alias) {
		BlogArticleCategoryExample example = new BlogArticleCategoryExample();
		example.createCriteria().andAliasEqualTo(alias);
		List<BlogArticleCategory> list = categoryMapper.selectByExample(example);
		if(list==null||list.size()==0){
			return null;
		}
		return list.get(0).getName();
	}

}
